package com.example.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private EditText et_user;
	private EditText et_password;
	private Button btn_login;
	private TextView tv_register;
	private CheckBox check_remember;
	private CheckBox check_login;
	private SharedPreferences sp;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		et_user = (EditText) findViewById(R.id.et_user);
		et_password = (EditText) findViewById(R.id.et_password);
		btn_login = (Button) findViewById(R.id.btn_login);
		tv_register = (TextView) findViewById(R.id.tv_register);
		check_remember = (CheckBox) findViewById(R.id.check_remember);
		
//		check_login = (CheckBox) findViewById(R.id.check_login);
		sp = getSharedPreferences("userinfo", 0);
		if (sp.getBoolean("IS", true)) {
			check_remember.setChecked(true);
			et_user.setText(sp.getString("USER", ""));
			et_password.setText(sp.getString("PASSWORD", ""));
		}
//		if (sp.getBoolean("login_check", true)){
//			Intent intent = new Intent(MainActivity.this, Logining.class);
//			startActivity(intent);
//		}
		tv_register.setOnClickListener(this);
		btn_login.setOnClickListener(this);
//		check_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				// TODO Auto-generated method stub
//				if(check_login.isChecked()){
//					sp.edit().putBoolean("login_check", true).commit();
//					Log.i("hahahahahaha","自动登录点击了～～～～～～～～～～～～～～～～～～");
//				}else{
//					sp.edit().putBoolean("login_check", false).commit();
//					Log.i("hahahahahaha","自动登录取消了～～～～～～～～～～～～～～～～～～");
//				}
//			}
//		});
		check_remember.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  
	            if (check_remember.isChecked()) {  
	                  
	                System.out.println("记住密码已选中");  
	                sp.edit().putBoolean("IS", true).commit();  
	                  
	            }else {  
	                  
	                System.out.println("记住密码没有选中");  
	                sp.edit().putBoolean("IS", false).commit();  
	                  
	            }  

	        }  
	    });
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			String username_information = et_user.getText().toString();
			String password_information = et_password.getText().toString();
			if (username_information.equals("lanou3g") && password_information.equals("123")) {
				
				if (check_remember.isChecked()) {
					Editor editor = sp.edit();
					editor.putString("USER", username_information);
					editor.putString("PASSWORD", password_information);
					editor.commit();
				}
				Intent intent = new Intent(MainActivity.this, Tabhost.class);
				startActivity(intent);
				finish();
			} else {
				Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.tv_register:
			Intent intent2 = new Intent(MainActivity.this,RegisterActivity.class);
			String str = et_user.getText().toString();
			intent2.putExtra("user", str);
			startActivityForResult(intent2, 100);
			break;
		}
	}
		// TODO Auto-generated method stub
	
		
	  
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		String str = data.getStringExtra("userback");
		et_user.setText(str);
		String str1 = data.getStringExtra("passwordback");
		et_password.setText(str1);
		Toast.makeText(MainActivity.this,"注册成功" , Toast.LENGTH_SHORT).show();
	}
	
	
	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){   
	        if((java.lang.System.currentTimeMillis()-exitTime) > 2000){  
	            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                
	            exitTime = java.lang.System.currentTimeMillis();
//	            Log.i("11", ()keyCode);
//	            Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
	        } else {
	            java.lang.System.exit(0);
	        }
	        return true;   
	    }
	    return super.onKeyDown(keyCode, event);
	}
	


	
}
